<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"blog.liu-tm.cn","root":"/","scheme":"Mist","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="[TOC] Spring A">
<meta property="og:type" content="article">
<meta property="og:title" content="Spring 问答">
<meta property="og:url" content="https://blog.liu-tm.cn/2018/10/05/Spring_Interview/index.html">
<meta property="og:site_name" content="Marvin&#39;s Blog">
<meta property="og:description" content="[TOC] Spring A">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2018-10-05T00:25:55.000Z">
<meta property="article:modified_time" content="2021-06-04T00:36:31.458Z">
<meta property="article:author" content="Marvin">
<meta property="article:tag" content="spring">
<meta property="article:tag" content="interview">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://blog.liu-tm.cn/2018/10/05/Spring_Interview/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Spring 问答 | Marvin's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body  oncontextmenu=self.event.returnValue=false onselectstart="return false"
 itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Marvin's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Hi, My friend. Welcome to my space</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>


    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://blog.liu-tm.cn/2018/10/05/Spring_Interview/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Marvin">
      <meta itemprop="description" content="博闻天地">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Marvin's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Spring 问答
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2018-10-05 08:25:55" itemprop="dateCreated datePublished" datetime="2018-10-05T08:25:55+08:00">2018-10-05</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-06-04 08:36:31" itemprop="dateModified" datetime="2021-06-04T08:36:31+08:00">2021-06-04</time>
              </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2018/10/05/Spring_Interview/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2018/10/05/Spring_Interview/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>20k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>18 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h5 id=""><a href="#" class="headerlink" title=""></a></h5><p>[TOC]</p>
<h2 id="Spring-A"><a href="#Spring-A" class="headerlink" title="Spring A"></a>Spring A</h2><h3 id="Bean的作用域"><a href="#Bean的作用域" class="headerlink" title="Bean的作用域 "></a><a href="#">Bean的作用域 </a></h3><p>**<code>singleton</code>**Spring IoC容器中只会存在一个共享的Bean实例，无论有多少个Bean引用它，始终指向同一对象。Singleton作用域是Spring中的缺省作用域。</p>
<p>**<code>prototype</code>**每次通过Spring容器获取prototype定义的bean时，容器都将创建一个新的Bean实例，每个Bean实例都有自己的属性和状态，而singleton全局只有一个对象。</p>
<p>**<code>request</code>**在一次Http请求中，容器会返回该Bean的同一实例。而对不同的Http请求则会产生新的Bean，而且该bean仅在当前Http Request内有效。</p>
<p>**<code>session</code>**在一次Http Session中，容器会返回该Bean的同一实例。而对不同的Session请求则会创建新的实例，该bean实例仅在当前Session内有效。</p>
<p>**<code>global Session</code>**在一个全局的Http Session中，容器会返回该Bean的同一个实例，仅在使用portlet context时有效。</p>
<h3 id="Bean的生命周期"><a href="#Bean的生命周期" class="headerlink" title="Bean的生命周期"></a><a href="#">Bean的生命周期</a></h3><ul>
<li>实例化一个Bean，也就是我们通常说的new。</li>
<li>按照Spring上下文对实例化的Bean进行配置，也就是IOC注入。</li>
<li>如果这个Bean实现了BeanNameAware接口，会调用它实现的setBeanName(String beanId)方法，此处传递的是Spring配置文件中Bean的ID。</li>
<li>如果这个Bean实现了BeanFactoryAware接口，会调用它实现的setBeanFactory()，传递的是Spring工厂本身（可以用这个方法获取到其他Bean）。</li>
<li>如果这个Bean实现了ApplicationContextAware接口，会调用setApplicationContext(ApplicationContext)方法，传入Spring上下文。</li>
<li>如果这个Bean关联了BeanPostProcessor接口，将会调用postProcessBeforeInitialization(Object obj, String s)方法，BeanPostProcessor经常被用作是Bean内容的更改，并且由于这个是在Bean初始化结束时调用After方法，也可用于内存或缓存技术。</li>
<li>如果这个Bean在Spring配置文件中配置了init-method属性会自动调用其配置的初始化方法。</li>
<li>如果这个Bean关联了BeanPostProcessor接口，将会调用postAfterInitialization(Object obj, String s)方法。</li>
<li>当Bean不再需要时，会经过清理阶段，如果Bean实现了DisposableBean接口，会调用其实现的destroy方法。</li>
<li>最后，如果这个Bean的Spring配置中配置了destroy-method属性，会自动调用其配置的销毁方法。</li>
</ul>
<h3 id="Spring中依赖注入两种方式"><a href="#Spring中依赖注入两种方式" class="headerlink" title="Spring中依赖注入两种方式"></a><a href="#">Spring中依赖注入两种方式</a></h3><p>两种注入方式为：构造方法注入和设值注入</p>
<ol>
<li>设值注入与传统的JavaBean的写法更相似，程序员更容易理解、接受，通过setter方式设定依赖关系显得更加直观、明显;</li>
<li>对于复杂的依赖关系，如果采用构造注入，会导致构造器过于臃肿，难以阅读。Spring在创建Bean实例时，需要同时实例化其依赖的全部实例，因而会产生浪费。而使用设值注入，则避免这下问题;</li>
<li>在某些属性可选的情况下，多参数的构造器更加笨拙，官方更鼓励使用设值注入。</li>
<li>构造注入可以在构造器中决定依赖关系的注入顺序，优先依赖的优先注入。</li>
<li>对于依赖关系无须变化的Bean，构造注入更有用处，因为没有setter方法，所有的依赖关系全部在构造器内设定，因此，不用担心后续代码对依赖关系的破坏。</li>
<li>构造注入使依赖关系只能在构造器中设定，则只有组件的创建者才能改变组件的依赖关系。对组件的调用者而言，组件内部的依赖关系完全透明，更符合高内聚的原则。</li>
<li>设值注入不会重写构造方法的值。如果我们对同一个变量同时使用了构造方法注入又使用了设置方法注入的话，那么构造方法将不能覆盖由设值方法注入的值。</li>
<li>建议采用以设值注入为主，构造注入为辅的注入策略。对于依赖关系无须变化的注入，尽量采用构造注入;而其他的依赖关系的注入，则考虑采用set注入。</li>
</ol>
<h3 id="Spring中的设计模式"><a href="#Spring中的设计模式" class="headerlink" title="Spring中的设计模式"></a><a href="#">Spring中的设计模式</a></h3><ul>
<li>代理模式：在AOP和remoting中被用的比较多。</li>
<li>单例模式：在spring配置文件中定义的bean默认为单例模式。</li>
<li>模板方法模式：用来解决代码重复的问题。</li>
<li>前端控制器模式：Spring提供了DispatcherServlet来对请求进行分发。</li>
<li>依赖注入模式：贯穿于BeanFactory / ApplicationContext接口的核心理念。</li>
<li>工厂模式：BeanFactory用来创建对象的实例。</li>
</ul>
<h3 id="BeanFactory-和ApplicationContext的区别"><a href="#BeanFactory-和ApplicationContext的区别" class="headerlink" title="BeanFactory 和ApplicationContext的区别"></a><a href="#">BeanFactory 和ApplicationContext的区别</a></h3><p>BeanFactory和ApplicationContext都是接口，并且ApplicationContext是BeanFactory的子接口。</p>
<p>BeanFactory是Spring中最底层的接口，提供了最简单的容器的功能，只提供了实例化对象和拿对象的功能。而ApplicationContext是Spring的一个更高级的容器，提供了更多的有用的功能。 </p>
<p>ApplicationContext提供的额外的功能：国际化的功能、消息发送、响应机制、统一加载资源的功能、强大的事件机制、对Web应用的支持等等。</p>
<p>加载方式的区别：BeanFactory采用的是延迟加载的形式来注入Bean；ApplicationContext则相反的，它是在Ioc启动时就一次性创建所有的Bean,好处是可以马上发现Spring配置文件中的错误，坏处是造成浪费。</p>
<h2 id="Spring-B"><a href="#Spring-B" class="headerlink" title="Spring B"></a>Spring B</h2><h3 id="1-为什么使用Spring"><a href="#1-为什么使用Spring" class="headerlink" title="1. 为什么使用Spring"></a>1. <a href="#">为什么使用Spring</a></h3><ol>
<li><p>方便解耦，简化开发</p>
<blockquote>
<p>通过Spring提供的IoC容器，可以将对象之间的依赖关系交由Spring进行控制，避免硬编码所造成的过度程序耦合。</p>
</blockquote>
</li>
<li><p>AOP编程的支持</p>
<blockquote>
<p>通过Spring提供的AOP功能，方便进行面向切面的编程，如性能监测、事务管理、日志记录等。</p>
</blockquote>
</li>
<li><p>声明式事务的支持</p>
</li>
<li><p>方便集成各种优秀框架</p>
</li>
<li><p>降低Java EE API的使用难度</p>
<blockquote>
<p>如对JDBC，JavaMail，远程调用等提供了简便封装。</p>
</blockquote>
</li>
</ol>
<h3 id="2-Spring的优点"><a href="#2-Spring的优点" class="headerlink" title="2. Spring的优点"></a>2. <a href="#">Spring的优点</a></h3><ol>
<li><p>降低了组件之间的耦合性 ，实现了软件各层之间的解耦</p>
</li>
<li><p>可以使用容易提供的众多服务，如事务管理，消息服务等</p>
</li>
<li><p>容器提供单例模式支持</p>
</li>
<li><p>容器提供了AOP技术，利用它很容易实现如权限拦截，运行期监控等功能</p>
</li>
<li><p>容器提供了众多的辅助类，能加快应用的开发</p>
</li>
<li><p>spring对于主流的应用框架提供了集成支持，如hibernate，JPA，Struts等</p>
</li>
<li><p>spring属于低侵入式设计，代码的污染极低</p>
</li>
<li><p>独立于各种应用服务器</p>
</li>
<li><p>spring的DI机制降低了业务对象替换的复杂性</p>
</li>
<li><p>Spring的高度开放性，并不强制应用完全依赖于Spring，开发者可以自由选择spring的部分或全部</p>
</li>
</ol>
<h3 id="3-什么是IOC，什么是DI，两者区别"><a href="#3-什么是IOC，什么是DI，两者区别" class="headerlink" title="3. 什么是IOC，什么是DI，两者区别"></a>3. <a href="#">什么是IOC，什么是DI，两者区别</a></h3><ul>
<li>DI：依赖注入(<strong>Dependency injection</strong>)</li>
<li>IOC：控制反转(<strong>Iversion of Controller</strong>)</li>
</ul>
<blockquote>
<p>依赖注入是一个程序设计模式和架构模型， 一些时候也称作控制反转，尽管在技术上来讲，依赖注入是一个IOC的特殊实现，依赖注入是指一个对象应用另外一个对象来提供一个特殊的能力，例如：把一个 数据库连接以参数的形式传到一个对象的结构方法里面而不是在那个对象内部自行创建一个连接。</p>
<p>控制反转和依赖注入的基本思想就是把类的依赖从类内部转化到外部以减少依赖</p>
<p>应用控制反转，对象在被创建的时候，由一个调控系统内所有对象的外界实体，将其所依赖的对象的引用，传递给它。也可以说，依赖被注入到对象中。所以，控制反转是，关于一个对象如何获取他所依赖的对象的引用，这个责任的反转。</p>
</blockquote>
<h3 id="4-什么是IoC，为什么使用IoC"><a href="#4-什么是IoC，为什么使用IoC" class="headerlink" title="4. 什么是IoC，为什么使用IoC"></a>4. <a href="#">什么是IoC，为什么使用IoC</a></h3><blockquote>
<p>IoC全称Iversion of Controller，控制反转。</p>
<p>当某个角色需要另外一个角色协助的时候，在传统的程序设计过程中，通常由调用者来创建被调用者的实例对象。但在spring中创建被调用者的工作不再由调用者来完成，因此称为控制反转。创建被调用者的工作由spring来完成，然后注入调用者 直接使用。</p>
<p>它能指导我们如何设计出松耦合、更优良的程序。</p>
</blockquote>
<h3 id="5-什么是AOP，为什么使用AOP"><a href="#5-什么是AOP，为什么使用AOP" class="headerlink" title="5. 什么是AOP，为什么使用AOP"></a>5. <a href="#">什么是AOP，为什么使用AOP</a></h3><blockquote>
<p>AOP全称：Aspect-Oriented Programming，面向切面编程。</p>
<p>AOP，面向切面编程，就是把可重用的功能提取出来，然后将这些通用功能在合适的时候织入到应用程序中，比如事务管理、权限控制、日志记录、性能统计等。</p>
<p>AOP并没有帮助我们解决任何新的问题，它只是提供了一种更好的办法，能够用更少的工作量来解决现有的一些问题，使得系统更加健壮，可维护性更好。</p>
</blockquote>
<h3 id="6-Spring中AOP的应用场景、Aop原理、好处"><a href="#6-Spring中AOP的应用场景、Aop原理、好处" class="headerlink" title="6. Spring中AOP的应用场景、Aop原理、好处"></a>6. <a href="#">Spring中AOP的应用场景、Aop原理、好处</a></h3><p>AOP=&gt;<strong>Aspect Oriented Programming</strong>面向切面编程；用来封装横切关注点，具体可以在下面的场景中使用:</p>
<p>Authentication 权限、Caching 缓存、Context passing 内容传递、Error handling 错误处理Lazy loading懒加载、Debugging调试、logging, tracing, profiling and monitoring 记录跟踪优化　校准、Performance optimization　性能优化、Persistence 持久化、Resource pooling　资源池、Synchronization　同步、Transactions 事务。</p>
<p>原理：AOP是面向切面编程，是通过<code>动态代理</code>的方式为程序添加统一功能，集中解决一些公共问题。</p>
<p>优点：</p>
<ul>
<li>各个步骤之间的良好隔离性耦合性大大降低</li>
<li>源代码无关性，再扩展功能的同时不对源码进行修改操作</li>
</ul>
<h3 id="7-什么是Spring的事务管理"><a href="#7-什么是Spring的事务管理" class="headerlink" title="7. 什么是Spring的事务管理"></a>7. <a href="#">什么是Spring的事务管理</a></h3><blockquote>
<p>事务就是对一系列的数据库操作（比如插入多条数据）进行统一的提交或回滚操作，如果插入成功，那么一起成功，如果中间有一条出现异常，那么回滚之前的所有操作。这样可以防止出现脏数据，防止数据库数据出现问题。</p>
<p>开发中为了避免这种情况一般都会进行事务管理。</p>
<p>Spring的声明式事务通常是指在配置文件中对事务进行配置声明，其中包括了很多声明属性，它是通过Spring Proxy帮你做代理，自己不用额外的写代码，只要在Spring配置文件中声明即可；通常用在数据库的操作里面；</p>
<p>编程式事务就是指通过硬编码的方式做事务处理，这种处理方式需要写代码，事务中的逻辑可以自己定制；可以是数据库的东东，也可以是其他的操作。<br>Spring中也有自己的事务管理机制，一般是使用TransactionMananger进行管理，可以通过Spring的注入来完成此功能。</p>
</blockquote>
<h3 id="8-Spring中Bean的作用域"><a href="#8-Spring中Bean的作用域" class="headerlink" title="8. Spring中Bean的作用域"></a>8. <a href="#">Spring中Bean的作用域</a></h3><ul>
<li>singleton : 默认值，bean在每个Spring ioc 容器中只有一个实例。</li>
<li>prototype：一个bean的定义可以有多个实例。</li>
<li>request：每次http请求都会创建一个bean，该作用域仅在基于web的Spring ApplicationContext情形下有效。</li>
<li>session：在一个HTTP Session中，一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。</li>
<li>global-session：在一个全局的HTTP Session中，一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。</li>
</ul>
<h3 id="9-什么是Spring的MVC框架"><a href="#9-什么是Spring的MVC框架" class="headerlink" title="9. 什么是Spring的MVC框架"></a>9. <a href="#">什么是Spring的MVC框架</a></h3><blockquote>
<p>Spring 配备构建Web 应用的全功能MVC框架。Spring可以很便捷地和其他MVC框架集成，如Struts，Spring 的MVC框架用控制反转把业务对象和控制逻辑清晰地隔离。它也允许以声明的方式把请求参数和业务对象绑定。</p>
<p>spring mvc是一个基于mvc的web框架。spring mvc是spring框架的一个模块，springmvc和spring无需通过中间整合层进行整合。</p>
</blockquote>
<h3 id="10-Spring-MVC的请求流程"><a href="#10-Spring-MVC的请求流程" class="headerlink" title="10. Spring MVC的请求流程"></a>10. <a href="#">Spring MVC的请求流程</a></h3><ul>
<li>第一步：发起请求到前端控制器(DispatcherServlet)</li>
<li>第二步：前端控制器请求HandlerMapping查找Handler可以根据xml配置、注解进行查找</li>
<li>第三步：处理器映射器HandlerMapping向前端控制器返回Handler</li>
<li>第四步：前端控制器调用处理器适配器去执行Handler</li>
<li>第五步：处理器适配器去执行Handler</li>
<li>第六步：Handler执行完成给适配器返回ModelAndView</li>
<li>第七步：处理器适配器向前端控制器返回ModelAndView。ModelAndView是springmvc框架的一个底层对象，包括 Model和view</li>
<li>第八步：前端控制器请求视图解析器去进行视图解析，根据逻辑视图名解析成真正的视图(jsp)</li>
<li>第九步：视图解析器向前端控制器返回View</li>
<li>第十步：前端控制器进行视图渲染。视图渲染将模型数据(在ModelAndView对象中)填充到request域</li>
<li>第十一步：前端控制器向用户响应结果</li>
</ul>
<h3 id="11-Springmvc的优点"><a href="#11-Springmvc的优点" class="headerlink" title="11. Springmvc的优点"></a>11. <a href="#">Springmvc的优点</a></h3><ol>
<li><p>它是基于组件技术的.全部的应用对象,无论控制器和视图,还是业务对象之类的都是 java组件.并且和Spring提供的其他基础结构紧密集成.</p>
</li>
<li><p>不依赖于Servlet API(目标虽是如此,但是在实现的时候确实是依赖于Servlet的)</p>
</li>
<li><p>可以任意使用各种视图技术,而不仅仅局限于JSP</p>
</li>
<li><p>支持各种请求资源的映射策略</p>
</li>
<li><p>它应是易于扩展的</p>
</li>
</ol>
<h3 id="12-Spring-MVC-和-Struts2-对比"><a href="#12-Spring-MVC-和-Struts2-对比" class="headerlink" title="12.Spring MVC 和 Struts2 对比"></a>12.<a href="#">Spring MVC 和 Struts2 对比</a></h3><ol>
<li><p>Struts2是类级别的拦截， 一个类对应一个request上下文，SpringMVC是方法级别的拦截，一个方法对应一个request上下文，而方法同时又跟一个url对应，所以说从架构本身上SpringMVC 就容易实现restful url</p>
</li>
<li><p>由上边原因，SpringMVC的方法之间基本上独立的，独享request response数据，请求数据通过参数获取，处理结果通过ModelMap交回给框架，方法之间不共享变量，而Struts2搞的就比较乱，虽然方法之间也是独立的，但其所有Action变量是共享的，这不会影响程序运行，却给我们编码 读程序时带来麻烦，每次来了请求就创建一个Action，一个Action对象对应一个request上下文。</p>
</li>
<li><p>由于Struts2需要针对每个request进行封装，把request，session等servlet生命周期的变量封装成一个一个Map，供给每个Action使用，并保证线程安全，所以在原则上，是比较耗费内存的。</p>
</li>
<li><p>SpringMVC集成了Ajax，使用非常方便，只需一个注解@ResponseBody就可以实现，然后直接返回响应文本即可，而Struts2拦截器集成了Ajax，在Action中处理时一般必须安装插件或者自己写代码集成进去，使用起来也相对不方便。</p>
</li>
<li><p>springmvc面向方法开发的（更接近service接口的开发方式），struts2面向类开发。</p>
</li>
<li><p>springmvc可以单例开发，struts2只能是多例开发。</p>
</li>
</ol>
<h3 id="13-如何启用注解"><a href="#13-如何启用注解" class="headerlink" title="13. 如何启用注解"></a>13. <a href="#">如何启用注解</a></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:annotation-config</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;com.tgb.web.controller.annotation&quot;</span>&gt;</span> </span><br><span class="line"><span class="comment">&lt;!-- 如果使用则上面内容&lt;context:annotation-config/&gt;可以省略--&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="14-web-xml的配置"><a href="#14-web-xml的配置" class="headerlink" title="14. web.xml的配置"></a>14. <a href="#">web.xml的配置</a></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--springmvc前端控制器 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>spring<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>org.springframework.web.servlet.DispatcherServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- </span></span><br><span class="line"><span class="comment">    contextConfigLocation配置的springmvc加载的配置文件（配置处理器映射器/适配器等）</span></span><br><span class="line"><span class="comment">    如果不配置contextConfigLocation，默认加载的是/WEB-INF/servlet名称-servlet.xml（springmvc-servlet.xml）</span></span><br><span class="line"><span class="comment">  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">init-param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>classpath:springmvc.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">init-param</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">load-on-startup</span>&gt;</span>1<span class="tag">&lt;/<span class="name">load-on-startup</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>springmvc<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">    第一种：*.do，访问以.do结尾由DispatcherServlet进行解析</span></span><br><span class="line"><span class="comment">    第二种：/，所有访问的地址都由DispatcherServlet进行解析，对于静态文件的解析需要配置不让DispatcherServlet进行解析此种方式可以实现RESTful风格的url</span></span><br><span class="line"><span class="comment">    第三种：/*，这样配置不对，使用这种配置，最终要转发到一个jsp页面时，仍然会由DispatcherServlet解析jsp地址，不能根据jsp页面找到handler，会报错</span></span><br><span class="line"><span class="comment">  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>*.do<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="15-注解的处理器映射器和适配器"><a href="#15-注解的处理器映射器和适配器" class="headerlink" title="15. 注解的处理器映射器和适配器"></a>15. <a href="#">注解的处理器映射器和适配器</a></h3><p>spring3.1之前使用<code>org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping</code>注解映射器。</p>
<p>spring3.1之后使用<code>org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter</code>注解适配器。</p>
<p>使用 <code>mvc:annotation-driven</code>代替上边注解映射器和注解适配器配置</p>
<h3 id="16-spring-与-mybatis整合过程"><a href="#16-spring-与-mybatis整合过程" class="headerlink" title="16. spring 与 mybatis整合过程"></a>16. <a href="#">spring 与 mybatis整合过程</a></h3><ol>
<li><p>整合dao层</p>
<p>mybatis和spring整合，通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口在spring中进行注册。</p>
</li>
<li><p>整合service层</p>
<p>通过spring管理 service接口。使用配置方式将service接口配置在spring配置文件中。实现事务控制。</p>
</li>
<li><p>整合springmvc</p>
<p>由于springmvc是spring的模块，不需要整合</p>
<p>主要配置有：</p>
</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">1). mybatis配置文件sqlMapConfig.xml配置别名自动扫描(实体类)</span><br><span class="line">2). mapper扫描器(接口，数据库访问接口)</span><br><span class="line">3). 数据库连接池配置</span><br><span class="line">4). 声明式事务配置</span><br><span class="line">5). 启用注解扫描：<span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">&quot;cn.itcast.ssm.controller&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">context:component-scan</span>&gt;</span></span><br><span class="line">6). 配置注解映射器和适配器： <span class="tag">&lt;<span class="name">mvc:annotation-driven</span>&gt;</span><span class="tag">&lt;/<span class="name">mvc:annotation-driven</span>&gt;</span></span><br><span class="line">7). 视图解析器：<span class="tag">&lt;<span class="name">bean</span>  <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;</span>&gt;</span></span><br><span class="line">8). 配置控制类： DispatcherServlet前端控制器</span><br><span class="line">9). 配置spring配置文件加载类：ClassLoadListener</span><br></pre></td></tr></table></figure>
<h3 id="17-视图解析器配置前缀和后缀"><a href="#17-视图解析器配置前缀和后缀" class="headerlink" title="17. 视图解析器配置前缀和后缀"></a>17. <a href="#">视图解析器配置前缀和后缀</a></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 视图解析器解析jsp时，默认使用jstl标签，classpath下的得有jstl的包--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span>&gt;</span></span><br><span class="line">  class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;</span><br><span class="line">  <span class="comment">&lt;!--配置jsp路径的前缀--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;prefix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;/WEB-INF/jsp/&quot;</span>/&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--配置jsp路径的后缀--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;suffix&quot;</span> <span class="attr">value</span>=<span class="string">&quot;.jsp&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="18-sqlMapConfig-xml，mybatis自己的配置文件"><a href="#18-sqlMapConfig-xml，mybatis自己的配置文件" class="headerlink" title="18. sqlMapConfig.xml，mybatis自己的配置文件"></a>18. <a href="#">sqlMapConfig.xml，mybatis自己的配置文件</a></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta">PUBLIC <span class="meta-string">&quot;-//mybatis.org//DTD Config 3.0//EN&quot;</span></span></span><br><span class="line"><span class="meta"><span class="meta-string">&quot;http://mybatis.org/dtd/mybatis-3-config.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span>     </span><br><span class="line">  <span class="comment">&lt;!--全局setting配置，根据需要添加--&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!--配置别名--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">typeAliases</span>&gt;</span>     </span><br><span class="line">    <span class="comment">&lt;!--批量扫描包名--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">package</span> <span class="attr">name</span>=<span class="string">&quot;com.mz.ssm.po&quot;</span> /&gt;</span>    </span><br><span class="line">  <span class="tag">&lt;/<span class="name">typeAliases</span>&gt;</span> </span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!--配置mapper</span></span><br><span class="line"><span class="comment">  由于使用spring和mybatis的整合进行mapper扫描，这里不需要配置了</span></span><br><span class="line"><span class="comment">  必须遵循mapper.xml和mapper.java文件同名且在一个目录</span></span><br><span class="line"><span class="comment">  --&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- &lt;mappers&gt;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">  &lt;/mappers&gt; --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="19-配置数据源"><a href="#19-配置数据源" class="headerlink" title="19. 配置数据源"></a>19. <a href="#">配置数据源</a></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 加载db.properties文件中的内容，db.properties文件中key命名有一定的特殊规则 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">&quot;classpath:db.properties&quot;</span> /&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 配置数据源，dbcp --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.apache.commons.dbcp.BasicDataSource&quot;</span></span></span><br><span class="line"><span class="tag">  <span class="attr">destroy-method</span>=<span class="string">&quot;close&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driverClassName&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.driver&#125;&quot;</span> /&gt;</span> </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.url&#125;&quot;</span> /&gt;</span> </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.username&#125;&quot;</span> /&gt;</span> </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;$&#123;jdbc.password&#125;&quot;</span> /&gt;</span> </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;maxActive&quot;</span> <span class="attr">value</span>=<span class="string">&quot;30&quot;</span> /&gt;</span> </span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;maxIdle&quot;</span> <span class="attr">value</span>=<span class="string">&quot;5&quot;</span> /&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- sqlSessionFactory --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;sqlSessionFactory&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 数据库连接池 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span> /&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 加载mybatis的全局配置文件 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;configLocation value=&quot;</span><span class="attr">classpath:mybatis</span>/<span class="attr">sqlMapConfig.xml</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- mapper扫描器 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 扫描包路径，如果需要扫描多个包，中间使用半角逗号隔开 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;basePackage&quot;</span> <span class="attr">value</span>=<span class="string">&quot;cn.mz.ssm.mapper&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;sqlSessionFactoryBeanName&quot;</span> <span class="attr">value</span>=<span class="string">&quot;sqlSessionFactory&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="20-事务控制-applicationContext-transaction-xml"><a href="#20-事务控制-applicationContext-transaction-xml" class="headerlink" title="20. 事务控制(applicationContext-transaction.xml)"></a>20. <a href="#">事务控制(applicationContext-transaction.xml)</a></h3><p>在<code>applicationContext-transaction.xml</code>中使用spring声明式事务控制方法。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 事务管理器</span></span><br><span class="line"><span class="comment">  对mybatis操作数据库事物控制，spring使用jdbc的事物控制类</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;transactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 数据源</span></span><br><span class="line"><span class="comment">  dataSource在applicationContext-dao.xml中配置了</span></span><br><span class="line"><span class="comment">  --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 通知 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">transaction-manager</span>=<span class="string">&quot;transactionManager&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 传播行为 --&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;save*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;delete*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;insert*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;update*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;REQUIRED&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;find*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;SUPPORTS&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;get*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;SUPPORTS&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;select*&quot;</span> <span class="attr">propagation</span>=<span class="string">&quot;SUPPORTS&quot;</span> <span class="attr">read-only</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">tx:advice</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- aop --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">pointcut</span>=<span class="string">&quot;execution(* cn.mz.ssm.serviceImpl..(..))&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="21-加载spring配置"><a href="#21-加载spring配置" class="headerlink" title="21. 加载spring配置"></a>21. <a href="#">加载spring配置</a></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 加载spring容器 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">context-param</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>/WEB-INF/classes/spring/applicationContext-*.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">context-param</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">listener</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">listener-class</span>&gt;</span>org.springframework.web.context.contextConfigLocation <span class="tag">&lt;/<span class="name">listener-class</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">listener</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="22-静态资源访问不被拦截"><a href="#22-静态资源访问不被拦截" class="headerlink" title="22. 静态资源访问不被拦截"></a>22. <a href="#">静态资源访问不被拦截</a></h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">resources</span> <span class="attr">mapping</span>=<span class="string">&quot;/resources/**&quot;</span> <span class="attr">location</span>=<span class="string">&quot;/resources/&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resources</span> <span class="attr">mapping</span>=<span class="string">&quot;/images/**&quot;</span> <span class="attr">location</span>=<span class="string">&quot;/images/&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">resources</span> <span class="attr">mapping</span>=<span class="string">&quot;/js/**&quot;</span> <span class="attr">location</span>=<span class="string">&quot;/js/&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="23-RequestMapping的作用"><a href="#23-RequestMapping的作用" class="headerlink" title="23. @RequestMapping的作用"></a>23. <a href="#">@RequestMapping的作用</a></h3><ol>
<li><p>url映射</p>
</li>
<li><p>窄化请求映射</p>
</li>
<li><p>限制http请求方法</p>
</li>
</ol>
<h3 id="24-controller方法的返回值"><a href="#24-controller方法的返回值" class="headerlink" title="24. controller方法的返回值"></a>24. <a href="#">controller方法的返回值</a></h3><p>1 返回ModelAndView</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">需要方法结束时，定义ModelAndView，将model和view分别进行设置。 </span><br></pre></td></tr></table></figure>
<p>2 返回string</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">如果controller方法返回<span class="built_in">string</span>，</span><br><span class="line"><span class="number">1</span>). 表示返回逻辑视图名。真正视图(jsp路径)=前缀+逻辑视图名+后缀</span><br><span class="line"><span class="number">2</span>). redirect重定向：返回字符串格式为：<span class="string">&quot;redirect:queryItem.action&quot;</span></span><br><span class="line"><span class="number">3</span>). forward页面转发：返回字符串格式为：“forward:queryItem.action”</span><br></pre></td></tr></table></figure>
<p>3 返回void</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">在controller方法形参上可以定义request和response，使用request或 response指定响应结果：</span><br><span class="line">1). 使用request转向页面，如下：request.getRequestDispatcher(<span class="string">&quot;页面路径&quot;</span>).forward(request, response);</span><br><span class="line">2). 也可以通过response页面重定向：response.sendRedirect(<span class="string">&quot;url&quot;</span>)</span><br><span class="line">3). 也可以通过response指定响应结果，例如响应json数据如下：</span><br><span class="line">response.setCharacterEncoding(<span class="string">&quot;utf-8&quot;</span>);</span><br><span class="line">response.setContentType(<span class="string">&quot;application/json;charset=utf-8&quot;</span>);</span><br><span class="line">response.getWriter().write(<span class="string">&quot;json串&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="25-参数绑定"><a href="#25-参数绑定" class="headerlink" title="25. 参数绑定"></a>25. <a href="#">参数绑定</a></h3><ol>
<li>默认支持的类型</li>
</ol>
<p>直接在controller方法形参上定义下边类型的对象，就可以使用这些对象。在参数绑定过程中，如果遇到下边类型直接进行绑定。</p>
<ol>
<li><p>HttpServletRequest：通过request对象获取请求信息</p>
</li>
<li><p>HttpServletResponse：通过response处理响应信息</p>
</li>
<li><p>HttpSession：通过session对象得到session中存放的对象</p>
</li>
<li><p>Model/ModelMap：model是一个接口，modelMap是一个接口实现 。作用：将model数据填充到request域。</p>
</li>
<li><p>简单类型</p>
</li>
</ol>
<p>通过@RequestParam对简单类型的参数进行绑定。</p>
<p>如果不使用@RequestParam，要求request传入参数名称和controller方法的形参名称一致，方可绑定成功。</p>
<p>如果使用@RequestParam，不用限制request传入参数名称和controller方法的形参名称一致。</p>
<p>通过required属性指定参数是否必须要传入，如果设置为true，没有传入参数，会报错。</p>
<ol start="3">
<li>pojo绑定</li>
</ol>
<p>页面中input的name和controller的pojo形参中的属性名称一致，将页面中数据绑定到pojo。(usename,age；不需要user.username,user.age)</p>
<ol start="4">
<li>自定义参数绑定实现日期类型绑定</li>
</ol>
<p>对于controller形参中pojo对象，如果属性中有日期类型，需要自定义参数绑定。将请求日期数据串转成 日期类型，要转换的日期类型和pojo中日期属性的类型保持一致。</p>
<h3 id="26-乱码处理"><a href="#26-乱码处理" class="headerlink" title="26. 乱码处理"></a>26. <a href="#">乱码处理</a></h3><ol>
<li>post乱码</li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">在<span class="selector-tag">web</span><span class="selector-class">.xml</span>添加<span class="selector-tag">post</span>乱码<span class="selector-tag">filter</span>：<span class="selector-tag">CharacterEncodingFilter</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li><p>对于get请求中文参数出现乱码解决方法有两个：</p>
<p>a. 修改tomcat配置文件添加编码与工程编码一致，如下：</p>
</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">Connector</span> <span class="attr">URIEncoding</span>=<span class="string">&quot;utf-8&quot;</span> <span class="attr">connectionTimeout</span>=<span class="string">&quot;20000&quot;</span> <span class="attr">port</span>=<span class="string">&quot;8080&quot;</span> <span class="attr">protocol</span>=<span class="string">&quot;HTTP/1.1&quot;</span> <span class="attr">redirectPort</span>=<span class="string">&quot;8443&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>
<p> b. 对参数进行重新编码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ISO8859-1是tomcat默认编码，需要将tomcat编码后的内容按utf-8编码</span></span><br><span class="line">String userName = <span class="keyword">new</span> String(request.getParamter(<span class="string">&quot;userName&quot;</span>).getBytes(<span class="string">&quot;ISO8859-1&quot;</span>),<span class="string">&quot;utf-8&quot;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="27-集合类型绑定"><a href="#27-集合类型绑定" class="headerlink" title="27. 集合类型绑定"></a>27. <a href="#">集合类型绑定</a></h3><ol>
<li>数组绑定：</li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">controller</span>方法参数使用：(<span class="selector-tag">Integer</span><span class="selector-attr">[]</span> <span class="selector-tag">itemId</span>)</span><br><span class="line">页面统一使用<span class="selector-pseudo">:itemId</span> 作为<span class="selector-tag">name</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>list绑定：</li>
</ol>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">pojo</span>属性名为：<span class="selector-tag">itemsList</span></span><br><span class="line">页面：<span class="selector-tag">itemsList</span><span class="selector-attr">[index]</span>.属性名</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>map 绑定：</li>
</ol>
<figure class="highlight dart"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">pojo属性名为：</span><br><span class="line">    <span class="built_in">Map</span>&lt;<span class="built_in">String</span>, <span class="built_in">Object</span>&gt; itemInfo = <span class="keyword">new</span> HashMap&lt;<span class="built_in">String</span>, <span class="built_in">Object</span>&gt;(); </span><br><span class="line">页面： </span><br><span class="line">    &lt;td&gt;姓名：&lt;inputtype=<span class="string">&quot;text&quot;</span>name=<span class="string">&quot;itemInfo[&#x27;name&#x27;]&quot;</span>/&gt;</span><br></pre></td></tr></table></figure>
<h3 id="28-spring-校验"><a href="#28-spring-校验" class="headerlink" title="28. spring 校验"></a>28. <a href="#">spring 校验</a></h3><ol>
<li><p>项目中，通常使用较多是前端的校验，比如页面中js校验。对于安全要求较高点建议在服务端进行校验。</p>
</li>
<li><p>springmvc使用hibernate的校验框架validation(和hibernate没有任何关系)。</p>
</li>
</ol>
<p>校验思路：页面提交请求的参数，请求到controller方法中，使用validation进行校验。如果校验出错，将错误信息展示到页面。</p>
<h3 id="29-数据回显"><a href="#29-数据回显" class="headerlink" title="29. 数据回显"></a>29. <a href="#">数据回显</a></h3><ol>
<li><p><code>@ModelAttribute</code>还可以将方法的返回值传到页面：在方法上加注解@ModelAttribute</p>
</li>
<li><p>使用最简单方法使用model，可以不用@ModelAttribute：model.addAttribute(“id”, id);</p>
</li>
<li><p>springmvc默认对pojo数据进行回显。pojo数据传入controller方法后，springmvc自动将pojo数据放到request域，key等于pojo类型（首字母小写）</p>
</li>
<li><p>public String testParam(PrintWriter out, @RequestParam(“username”) String username) { //out直接输出</p>
</li>
</ol>
<h3 id="30-异常处理"><a href="#30-异常处理" class="headerlink" title="30. 异常处理"></a>30. <a href="#">异常处理</a></h3><p>springmvc提供全局异常处理器（一个系统只有一个异常处理器）进行统一异常处理。</p>
<p>系统遇到异常，在程序中手动抛出，dao抛给service、service给controller、controller抛给前端控制器，前端控制器调用全局异常处理器。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 全局异常处理器</span></span><br><span class="line"><span class="comment">  只要实现HandlerExceptionResolver接口就是全局异常处理器</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;cn.mz.ssm.exception.CustomExceptionResolver&gt;&lt;/bean&gt;</span></span></span><br></pre></td></tr></table></figure>
<h3 id="31-上传图片"><a href="#31-上传图片" class="headerlink" title="31. 上传图片"></a>31. <a href="#">上传图片</a></h3><ol>
<li><p>在页面form中提交enctype=”multipart/form-data”的数据时，需要springmvc对multipart类型的数据进行解析。</p>
</li>
<li><p>在springmvc.xml中配置multipart类型解析器。</p>
</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 文件上传 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;multipartResolver&quot;</span></span></span><br><span class="line"><span class="tag">  <span class="attr">class</span>=<span class="string">&quot;org.springframework.web.multipart.commons.CommonsMultipartResolver&quot;</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 设置上传文件的最大尺寸为5MB --&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;maxUploadSize&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">value</span>&gt;</span>5242880<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>3). 方法中使用：MultipartFile attach (单个文件上传) 或者  MultipartFile[] attachs (多个文件上传)</p>
<h3 id="32-Json处理"><a href="#32-Json处理" class="headerlink" title="32. Json处理"></a>32. <a href="#">Json处理</a></h3><ol>
<li><p>加载json转换的jar包：springmvc中使用jackson的包进行json转换（@requestBody和@responseBody使用下边的包进行json转）</p>
</li>
<li><p>配置json转换器。在注解适配器RequestMappingHandlerAdapter中加入messageConverters。如果使用&lt;mvc:annotation-driven /&gt; 则会自动加入。</p>
</li>
<li><p>ajax</p>
</li>
</ol>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//请求json，输出json</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">requestJson</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  $.ajax(&#123;</span><br><span class="line">    type:<span class="string">&#x27;post&#x27;</span>,</span><br><span class="line">    url:<span class="string">&#x27;$&#123;pageContext.request.contextPath&#125;/requestJson.do&#x27;</span>,</span><br><span class="line">    contentType:<span class="string">&#x27;application/json;charset=utf-8&#x27;</span>,</span><br><span class="line">    <span class="comment">//数据格式是json串，商品信息</span></span><br><span class="line">    data:<span class="string">&#x27;&#123;&quot;name&quot;:&quot;手机&quot;,&quot;price&quot;:999&#125;&#x27;</span>,</span><br><span class="line">    success:<span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>&#123;<span class="comment">//返回json结果</span></span><br><span class="line">      alert(data);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>Controller (ResponseBody、RequestBody)</li>
</ol>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//请求json串（商品信息），输出json（商品信息）</span></span><br><span class="line"><span class="comment">//@RequestBody将请求的商品信息的json串转成itemsCustom对象</span></span><br><span class="line"><span class="comment">//@ResponseBody将itemsCustom转成json输出</span></span><br><span class="line"><span class="meta">@RequestMapping(<span class="meta-string">&quot;/requestJson&quot;</span>)</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@ResponseBody</span> IntemsCustom requestJson(<span class="meta">@RequestBody</span> ItemCustom itemsCustom) &#123;</span><br><span class="line">  <span class="comment">//@ResponseBody将itemsCustom转成json输出</span></span><br><span class="line">  <span class="keyword">return</span> itemsCustom;</span><br></pre></td></tr></table></figure>
<ol start="5">
<li>注意ajax中contentType如果不设置为json类型，则传的参数为key/value类型。上面设置后，传的是json类型。</li>
</ol>
<h3 id="33-拦截器"><a href="#33-拦截器" class="headerlink" title="33. 拦截器"></a>33. <a href="#">拦截器</a></h3><ol>
<li><p>定义拦截器，实现HandlerInterceptor接口。接口中提供三个方法。</p>
<ol>
<li>preHandle ：进入 Handler方法之前执行，用于身份认证、身份授权，比如身份认证，如果认证通过表示当前用户没有登陆，需要此方法拦截不再向下执行</li>
<li>postHandle：进入Handler方法之后，返回modelAndView之前执行，应用场景从modelAndView出发：将公用的模型数据(比如菜单导航)在这里传到视图，也可以在这里统一指定视图</li>
<li>afterCompletion：执行Handler完成执行此方法，应用场景：统一异常处理，统一日志处理</li>
</ol>
</li>
<li><p>拦截器配置：</p>
<ul>
<li>针对HandlerMapping配置(不推荐)：springmvc拦截器针对HandlerMapping进行拦截设置，如果在某个HandlerMapping中配置拦截，经过该 HandlerMapping映射成功的handler最终使用该 拦截器。  (一般不推荐使用)</li>
<li>类似全局的拦截器：springmvc配置类似全局的拦截器，springmvc框架将配置的类似全局的拦截器注入到每个HandlerMapping中</li>
</ul>
</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 拦截器 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mvc:intercepters</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 多个拦截器，顺序执行 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">mvc:intercepter</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- /**表示所有url包括子url路径 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">mvc:mapping</span> <span class="attr">path</span>=<span class="string">&quot;/**&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;cn.mz.ssm.intercepter.HandlerIntercepter1&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">mvc:intercepter</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">mvc:intercepter</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">mvc:mapping</span> <span class="attr">path</span>=<span class="string">&quot;/**&quot;</span>/&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">&quot;cn.mz.ssm.intercepter.HandlerIntercepter2&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">mvc:intercepter</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">mvc:intercepters</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h3 id="34-spring中自动装配的方式有哪些"><a href="#34-spring中自动装配的方式有哪些" class="headerlink" title="34. spring中自动装配的方式有哪些"></a>34. <a href="#">spring中自动装配的方式有哪些</a></h3><ol>
<li><p>No：即不启用自动装配。</p>
</li>
<li><p>byName：通过属性的名字的方式查找JavaBean依赖的对象并为其注入。比如说类Computer有个属性printer，指定其autowire属性为byName后，Spring IoC容器会在配置文件中查找id/name属性为printer的bean，然后使用Seter方法为其注入。</p>
</li>
<li><p>byType：通过属性的类型查找JavaBean依赖的对象并为其注入。比如类Computer有个属性printer，类型为Printer，那么，指定其autowire属性为byType后，Spring IoC容器会查找Class属性为Printer的bean，使用Seter方法为其注入。</p>
</li>
<li><p>constructor：通byType一样，也是通过类型查找依赖对象。与byType的区别在于它不是使用Seter方法注入，而是使用构造子注入。</p>
</li>
<li><p>autodetect：在byType和constructor之间自动的选择注入方式。</p>
</li>
<li><p>default：由上级标签<beans>的default-autowire属性确定。</p>
</li>
</ol>
<h3 id="35-Spring常见创建对象的注解"><a href="#35-Spring常见创建对象的注解" class="headerlink" title="35. Spring常见创建对象的注解"></a>35. <a href="#">Spring常见创建对象的注解</a></h3><ul>
<li><code>@Component</code></li>
<li><code>@Controller</code></li>
<li><code>@Service</code></li>
<li><code>@Repository</code></li>
</ul>
<h3 id="36-Spring中的设计模式"><a href="#36-Spring中的设计模式" class="headerlink" title="36. Spring中的设计模式"></a>36. <a href="#">Spring中的设计模式</a></h3><blockquote>
<p>简单工厂、工厂方法、单例模式、适配器、包装器、代理、观察者、策略、模板方法</p>
</blockquote>
<ul>
<li>代理模式：在AOP和remoting中被用的比较多。</li>
<li>单例模式：在spring配置文件中定义的bean默认为单例模式。</li>
<li>模板方法模式：用来解决代码重复的问题。</li>
<li>前端控制器模式：Spring提供了DispatcherServlet来对请求进行分发。</li>
<li>依赖注入模式：贯穿于BeanFactory / ApplicationContext接口的核心理念。</li>
<li>工厂模式：BeanFactory用来创建对象的实例。</li>
</ul>
<h3 id="37-Spring管理事务"><a href="#37-Spring管理事务" class="headerlink" title="37. Spring管理事务"></a>37. <a href="#">Spring管理事务</a></h3><ol>
<li><p>编程式事务，在代码中硬编码。(不推荐使用)</p>
</li>
<li><p>声明式事务，在配置文件中配置（推荐使用）</p>
<ol>
<li><p>基于XML的声明式事务</p>
</li>
<li><p>基于注解的声明式事务</p>
</li>
</ol>
</li>
</ol>
<h3 id="38-spring中的核心类有那些，各有什么作用"><a href="#38-spring中的核心类有那些，各有什么作用" class="headerlink" title="38. spring中的核心类有那些，各有什么作用"></a>38. <a href="#">spring中的核心类有那些，各有什么作用</a></h3><ul>
<li>BeanFactory：产生一个新的实例，可以实现单例模式</li>
<li>BeanWrapper：提供统一的get及set方法</li>
<li>ApplicationContext:提供框架的实现，包括BeanFactory的所有功能</li>
</ul>
<h3 id="39-Bean的调用方式有哪些"><a href="#39-Bean的调用方式有哪些" class="headerlink" title="39. Bean的调用方式有哪些"></a>39. <a href="#">Bean的调用方式有哪些</a></h3><ol>
<li>使用BeanWrapper</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">HelloWorld hw = <span class="keyword">new</span> HelloWorld(); </span><br><span class="line">BeanWrapper bw = <span class="keyword">new</span> BeanWrapperImpl(hw); </span><br><span class="line">bw.setPropertyvalue(”msg”,”HelloWorld”);</span><br><span class="line">system.out.println(bw.getPropertyCalue(”msg”));</span><br></pre></td></tr></table></figure>
<ol start="2">
<li>使用BeanFactory</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">InputStream is = <span class="keyword">new</span> FileInputStream(”config.xml”);</span><br><span class="line">XmlBeanFactory factory = <span class="keyword">new</span> XmlBeanFactory(is);</span><br><span class="line">HelloWorld hw = (HelloWorld) factory.getBean(”HelloWorld”);</span><br><span class="line">system.out.println(hw.getMsg()); </span><br></pre></td></tr></table></figure>
<ol start="3">
<li>使用ApplicationConttext</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ApplicationContext actx = <span class="keyword">new</span> FleSystemXmlApplicationContext(”config.xml”);</span><br><span class="line">HelloWorld hw = (HelloWorld) actx.getBean(”HelloWorld”);</span><br><span class="line">System.out.println(hw.getMsg());</span><br></pre></td></tr></table></figure>
<h3 id="40-spring两种代理方式"><a href="#40-spring两种代理方式" class="headerlink" title="40. spring两种代理方式"></a>40. <a href="#">spring两种代理方式</a></h3><ul>
<li><p>若目标对象实现了若干接口，spring使用JDK的<code>java.lang.reflect.Proxy</code>类代理。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">优点：因为有接口，所以使系统更加松耦合</span><br><span class="line">缺点：为每一个目标类创建接口</span><br></pre></td></tr></table></figure>
</li>
<li><p>若目标对象没有实现任何接口，spring使用<code>CGLIB</code>库生成目标对象的子类。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">优点：因为代理类与目标类是继承关系，所以不需要有接口的存在。</span><br><span class="line">缺点：因为没有使用接口，所以系统的耦合性没有使用JDK的动态代理好。</span><br></pre></td></tr></table></figure>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
	  
	  	
		<div class="post-end">
		  
			<div>
    
	<div class="end-flag">

		<span class="line"></span>

		<span class="text">&ensp;本文到此结束&ensp;<i class="fa fa-heart"></i>&ensp;感谢您的阅读&ensp;</span>

		<span class="line"></span>

    </div>
<!--
        <div style="text-align:center;color: #ccc;font-size:14px;">------------&ensp;本文到此结束&ensp;<i class="fa fa-heart"></i>&ensp;感谢您的阅读&ensp;------------</div>		
-->
    
</div>
		  
		</div>
          <div class="post-tags">
              <a href="/tags/spring/" rel="tag"><i class="fa fa-tags"></i> spring</a>
              <a href="/tags/interview/" rel="tag"><i class="fa fa-tags"></i> interview</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item"></div>
      <div class="post-nav-item">
    <a href="/2020/12/12/Windows_mstsc/" rel="next" title="Windows Mstsc">
      Windows Mstsc <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>

	  	<div class="post-spread"></div>

	<link rel="stylesheet" href="/css/share.min.css" >
	<script src="/js/jquery-1.10.2.min.js"></script>
	<script src="/js/jquery.share.js"></script>
	<script src="/js/jquery.qrcode.min.js"></script>

	<script>
	var statement = "本文取自网络"
	+( $('#quote').data('modify') ? "（已作部分改动及格式整理），":"，")
	+( $('#quote').data('source') ? "原文来自<a id='source_href' href='javascript:getSource()'>这里</a>，":"")
	+"如有侵权请联系<a href='../../../../about/index'>我</a>改正。";
	$('#quote').html(statement);
	function getSource(){
		window.open($('#quote').data('source'))
	}
	</script>
	
	<script>
	var $config = {
	  sites : ['weibo','qq', 'wechat','qzone','douban', 'facebook', 'twitter'],
	  disabled: [ 'linkedin', 'diandian'],
	  wechatQrcodeTitle: "微信扫一扫",
	  wechatQrcodeHelper: '<p>微信扫一扫，右上角分享</p>',
	  source: 'Marvin'
	};


	$('.post-spread').share($config);

	function pjaxshare() {
	  $('.post-spread').share($config);
	}
	</script>	
	  
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
	  
	  <!--网易云音乐播放器-->
	  <iframe
	  frameborder="no" border="0" marginwidth="0" marginheight="0" 
	  width=300 height=450 
	  src="//music.163.com/outchain/player?type=0&id=5442729063&auto=0&height=430">
	  </iframe>

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link"><span class="nav-number">1.</span> <span class="nav-text"></span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-A"><span class="nav-number"></span> <span class="nav-text">Spring A</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Bean%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number"></span> <span class="nav-text">Bean的作用域 </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Bean%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="nav-number"></span> <span class="nav-text">Bean的生命周期</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring%E4%B8%AD%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F"><span class="nav-number"></span> <span class="nav-text">Spring中依赖注入两种方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Spring%E4%B8%AD%E7%9A%84%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F"><span class="nav-number"></span> <span class="nav-text">Spring中的设计模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#BeanFactory-%E5%92%8CApplicationContext%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-number"></span> <span class="nav-text">BeanFactory 和ApplicationContext的区别</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring-B"><span class="nav-number"></span> <span class="nav-text">Spring B</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BD%BF%E7%94%A8Spring"><span class="nav-number"></span> <span class="nav-text">1. 为什么使用Spring</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-Spring%E7%9A%84%E4%BC%98%E7%82%B9"><span class="nav-number"></span> <span class="nav-text">2. Spring的优点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E4%BB%80%E4%B9%88%E6%98%AFIOC%EF%BC%8C%E4%BB%80%E4%B9%88%E6%98%AFDI%EF%BC%8C%E4%B8%A4%E8%80%85%E5%8C%BA%E5%88%AB"><span class="nav-number"></span> <span class="nav-text">3. 什么是IOC，什么是DI，两者区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E4%BB%80%E4%B9%88%E6%98%AFIoC%EF%BC%8C%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BD%BF%E7%94%A8IoC"><span class="nav-number"></span> <span class="nav-text">4. 什么是IoC，为什么使用IoC</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E4%BB%80%E4%B9%88%E6%98%AFAOP%EF%BC%8C%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BD%BF%E7%94%A8AOP"><span class="nav-number"></span> <span class="nav-text">5. 什么是AOP，为什么使用AOP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-Spring%E4%B8%ADAOP%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF%E3%80%81Aop%E5%8E%9F%E7%90%86%E3%80%81%E5%A5%BD%E5%A4%84"><span class="nav-number"></span> <span class="nav-text">6. Spring中AOP的应用场景、Aop原理、好处</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-%E4%BB%80%E4%B9%88%E6%98%AFSpring%E7%9A%84%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86"><span class="nav-number"></span> <span class="nav-text">7. 什么是Spring的事务管理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-Spring%E4%B8%ADBean%E7%9A%84%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="nav-number"></span> <span class="nav-text">8. Spring中Bean的作用域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#9-%E4%BB%80%E4%B9%88%E6%98%AFSpring%E7%9A%84MVC%E6%A1%86%E6%9E%B6"><span class="nav-number"></span> <span class="nav-text">9. 什么是Spring的MVC框架</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#10-Spring-MVC%E7%9A%84%E8%AF%B7%E6%B1%82%E6%B5%81%E7%A8%8B"><span class="nav-number"></span> <span class="nav-text">10. Spring MVC的请求流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#11-Springmvc%E7%9A%84%E4%BC%98%E7%82%B9"><span class="nav-number"></span> <span class="nav-text">11. Springmvc的优点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#12-Spring-MVC-%E5%92%8C-Struts2-%E5%AF%B9%E6%AF%94"><span class="nav-number"></span> <span class="nav-text">12.Spring MVC 和 Struts2 对比</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#13-%E5%A6%82%E4%BD%95%E5%90%AF%E7%94%A8%E6%B3%A8%E8%A7%A3"><span class="nav-number"></span> <span class="nav-text">13. 如何启用注解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#14-web-xml%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="nav-number"></span> <span class="nav-text">14. web.xml的配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#15-%E6%B3%A8%E8%A7%A3%E7%9A%84%E5%A4%84%E7%90%86%E5%99%A8%E6%98%A0%E5%B0%84%E5%99%A8%E5%92%8C%E9%80%82%E9%85%8D%E5%99%A8"><span class="nav-number"></span> <span class="nav-text">15. 注解的处理器映射器和适配器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#16-spring-%E4%B8%8E-mybatis%E6%95%B4%E5%90%88%E8%BF%87%E7%A8%8B"><span class="nav-number"></span> <span class="nav-text">16. spring 与 mybatis整合过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#17-%E8%A7%86%E5%9B%BE%E8%A7%A3%E6%9E%90%E5%99%A8%E9%85%8D%E7%BD%AE%E5%89%8D%E7%BC%80%E5%92%8C%E5%90%8E%E7%BC%80"><span class="nav-number"></span> <span class="nav-text">17. 视图解析器配置前缀和后缀</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#18-sqlMapConfig-xml%EF%BC%8Cmybatis%E8%87%AA%E5%B7%B1%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="nav-number"></span> <span class="nav-text">18. sqlMapConfig.xml，mybatis自己的配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#19-%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E6%BA%90"><span class="nav-number"></span> <span class="nav-text">19. 配置数据源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#20-%E4%BA%8B%E5%8A%A1%E6%8E%A7%E5%88%B6-applicationContext-transaction-xml"><span class="nav-number"></span> <span class="nav-text">20. 事务控制(applicationContext-transaction.xml)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#21-%E5%8A%A0%E8%BD%BDspring%E9%85%8D%E7%BD%AE"><span class="nav-number"></span> <span class="nav-text">21. 加载spring配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#22-%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90%E8%AE%BF%E9%97%AE%E4%B8%8D%E8%A2%AB%E6%8B%A6%E6%88%AA"><span class="nav-number"></span> <span class="nav-text">22. 静态资源访问不被拦截</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#23-RequestMapping%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number"></span> <span class="nav-text">23. @RequestMapping的作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#24-controller%E6%96%B9%E6%B3%95%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC"><span class="nav-number"></span> <span class="nav-text">24. controller方法的返回值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#25-%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A"><span class="nav-number"></span> <span class="nav-text">25. 参数绑定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#26-%E4%B9%B1%E7%A0%81%E5%A4%84%E7%90%86"><span class="nav-number"></span> <span class="nav-text">26. 乱码处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#27-%E9%9B%86%E5%90%88%E7%B1%BB%E5%9E%8B%E7%BB%91%E5%AE%9A"><span class="nav-number"></span> <span class="nav-text">27. 集合类型绑定</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#28-spring-%E6%A0%A1%E9%AA%8C"><span class="nav-number"></span> <span class="nav-text">28. spring 校验</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#29-%E6%95%B0%E6%8D%AE%E5%9B%9E%E6%98%BE"><span class="nav-number"></span> <span class="nav-text">29. 数据回显</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#30-%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86"><span class="nav-number"></span> <span class="nav-text">30. 异常处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#31-%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87"><span class="nav-number"></span> <span class="nav-text">31. 上传图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#32-Json%E5%A4%84%E7%90%86"><span class="nav-number"></span> <span class="nav-text">32. Json处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#33-%E6%8B%A6%E6%88%AA%E5%99%A8"><span class="nav-number"></span> <span class="nav-text">33. 拦截器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#34-spring%E4%B8%AD%E8%87%AA%E5%8A%A8%E8%A3%85%E9%85%8D%E7%9A%84%E6%96%B9%E5%BC%8F%E6%9C%89%E5%93%AA%E4%BA%9B"><span class="nav-number"></span> <span class="nav-text">34. spring中自动装配的方式有哪些</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#35-Spring%E5%B8%B8%E8%A7%81%E5%88%9B%E5%BB%BA%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%B3%A8%E8%A7%A3"><span class="nav-number"></span> <span class="nav-text">35. Spring常见创建对象的注解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#36-Spring%E4%B8%AD%E7%9A%84%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F"><span class="nav-number"></span> <span class="nav-text">36. Spring中的设计模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#37-Spring%E7%AE%A1%E7%90%86%E4%BA%8B%E5%8A%A1"><span class="nav-number"></span> <span class="nav-text">37. Spring管理事务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#38-spring%E4%B8%AD%E7%9A%84%E6%A0%B8%E5%BF%83%E7%B1%BB%E6%9C%89%E9%82%A3%E4%BA%9B%EF%BC%8C%E5%90%84%E6%9C%89%E4%BB%80%E4%B9%88%E4%BD%9C%E7%94%A8"><span class="nav-number"></span> <span class="nav-text">38. spring中的核心类有那些，各有什么作用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#39-Bean%E7%9A%84%E8%B0%83%E7%94%A8%E6%96%B9%E5%BC%8F%E6%9C%89%E5%93%AA%E4%BA%9B"><span class="nav-number"></span> <span class="nav-text">39. Bean的调用方式有哪些</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#40-spring%E4%B8%A4%E7%A7%8D%E4%BB%A3%E7%90%86%E6%96%B9%E5%BC%8F"><span class="nav-number"></span> <span class="nav-text">40. spring两种代理方式</span></a></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Marvin"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Marvin</p>
  <div class="site-description" itemprop="description">博闻天地</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/liu-tm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;liu-tm" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:liumin@liu-tm.cn" title="E-Mail → mailto:liumin@liu-tm.cn" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://wpa.qq.com/msgrd?v=3&uin=352626862&site=qq&menu=yes" title="QQ → http:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;352626862&amp;site&#x3D;qq&amp;menu&#x3D;yes" rel="noopener" target="_blank"><i class="fa fa-user fa-fw"></i>QQ</a>
      </span>
  </div>



      </div>
	  
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  <span>Copyright </span>
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2021</span>
  
  <!--
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  -->
  
  <span>Author by </span>
  
  <span class="author" itemprop="copyrightHolder">Marvin</span>
</div>
<div>
	<!--
    <span class="post-meta-divider">|</span>
	-->
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">60k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">55 分钟</span>
</div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : true,
      appId      : '3GDAqGOhX31UHhUeEgKgOPrs-gzGzoHsz',
      appKey     : 'c0Q8up8LMo25TzptQjdEJoEW',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
